﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace com.sharepointutils.application.Forms
{
    public partial class QueryForm : Form
    {
        public QueryForm(SharePointConnection connection)
        {
            InitializeComponent();
            this.Connection = connection;
            this.List = connection.List;
            lbl_caml_current.Text = connection.Label;
            this.Tag = connection.Label;
            this.Text = "Query Builder - " + connection.List;

            //XMLViewerSettings viewerSetting = new XMLViewerSettings
            //{
            //    AttributeKey = Color.Red,
            //    AttributeValue = Color.Blue,
            //    Tag = Color.Blue,
            //    Element = Color.DarkRed,
            //    Value = Color.Black,
            //};
            //txt_caml_query.Settings = viewerSetting; 
        }

        public QueryForm(String associatedfile)
        {
            InitializeComponent();

            OpenFile(associatedfile);

            //XMLViewerSettings viewerSetting = new XMLViewerSettings
            //{
            //    AttributeKey = Color.Red,
            //    AttributeValue = Color.Blue,
            //    Tag = Color.Blue,
            //    Element = Color.DarkRed,
            //    Value = Color.Black,
            //};
            //txt_caml_query.Settings = viewerSetting;
            
        }


        public SharePointConnection Connection { get; set; }
        String List { get; set; }

        private void btn_caml_run_Click(object sender, EventArgs e)
        {
            try
            {
                if (Connection != null && !String.IsNullOrEmpty(List))
                {
                    DataTable dt = null;
                    if(checkBox_getAll.Checked)
                    {
                       // dt = SharePointUtilities.getFullListItemsReturnDataTable(Connection.URL, List, txt_caml_query.Text, Connection.Credentials);
                    }
                    else
                    {
                       // dt = SharePointUtilities.getAllListItemsReturnDataTable(Connection.URL, List, txt_caml_query.Text, Connection.Credentials);
                    }
                        
                    label_count.Text = "Count: " + dt.Rows.Count;
                    gv_caml_results.DataSource = dt;
                }
            }
            catch (Exception ex)
            {
                ((MainForm)this.MdiParent).ReportError(ex);
                label_count.Text = "Count: 0";
                gv_caml_results.DataSource = null;
            }
        }

        void txt_caml_query_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
        {
            if (e.Data.GetDataPresent("Text"))
            {
                e.Effect = DragDropEffects.Copy;
            }
        }

        void txt_caml_query_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
        {
            //txt_caml_query.SelectedText = e.Data.GetData("System.String", true).ToString();
        }

        private void dropTag(object sender, MouseEventArgs e)
        {
            Label label = sender as Label;
            DoDragDrop(label.Text, DragDropEffects.Move);
        }

        private void btn_save_Click(object sender, EventArgs e)
        {
            if (saveFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                using (Stream stream = saveFileDialog.OpenFile())
                {
                    using (StreamWriter writer = new StreamWriter(stream,Encoding.UTF8))
                    {
                       // writer.Write(txt_caml_query.Text);
                    }
                }
            }
        }

        private void btn_open_Click(object sender, EventArgs e)
        {
            if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                OpenFile(openFileDialog.FileName);

            }
        }

        private void OpenFile(String path)
        {
            using (Stream sr = File.Open(path, FileMode.Open))
            {
                using (StreamReader reader = new StreamReader(sr, Encoding.UTF8))
                {
                    //txt_caml_query.Text = reader.ReadToEnd();

                    //SET Connection Details
                    //this.Connection = connection;
                    //this.List = list;
                    //lbl_caml_current.Text = label;
                    //this.Tag = label;
                    //this.Text = "Query Builder - " + list;
                }
            }
        }

    }
}
